<?php   // $Id: devis_edit.php 347 2012-05-07 09:11:55Z pcoustillas $ ?>
<?php
/*
Copyright (C) 2003-2004 Guy Hendrickx
Licensed under the terms of the GNU  General Public License:http://www.opensource.org/licenses/gpl-license.php
File Authors:Guy Hendrickx 
*/
require_once("include/verif.php");
require_once("include/config/common.php");
require_once("include/config/var.php");
require_once("include/language/$lang.php");
require_once("include/utils.php");


if (isset($_GET['nouv']))
{
//cela veux dire que l'on doit créer un nouveau devis.
	$num_client=isset($_GET['num_client'])?$_GET['num_client']:0;
	if($num_client=='0')
	{
		header("Location: devis_lister.php?message=Vous n'avez pas choisi de client");
		exit;
	}

	$date=isset($_GET['date'])?$_GET['date']:"";
	list($jour, $mois,$annee) = preg_split('/\//', $date, 3);

	//mysql_select_db($db) or die ("Could not select $db database");
	$sql1 = "INSERT INTO ${tblpref}devis (client_num, date) VALUES ('$num_client', '$annee-$mois-$jour')";
	mysql_query($sql1) or die( "Erreur lors de la création du devis.<br/>$sql1<br/>" . mysql_error());

	//recuperer le num de devis le plus élevé, cela correspond au numero de devis fraichement crée.
	$sql = "SELECT MAX(num_dev) As Maxi FROM ${tblpref}devis";
	$result = mysql_query($sql) or die('Erreur selection du devis le plus grand');
	$num_devis = mysql_result($result, 0);

	//$rqSql = "SELECT num, article, prix_htva, uni FROM " . $tblpref ."article WHERE actif != 'non' ORDER BY article, prix_htva";
	//$result = mysql_query( $rqSql )	or die( "Erreur lors de la création du devis.<br/>$sql1<br/>" . mysql_error());


} else {
	//c'est une modif ou une consultation
	$num_devis=isset($_GET['num_devis'])?$_GET['num_devis']:$_POST['num_devis'];

	if (isset($_POST['coment']))
	{
		//la page à été appelé depuis une mise à jour de commentaire.
		//le commentaire doit donc etre mis à jour
		$sql_ht = "UPDATE ${tblpref}devis SET coment='" . $_POST['coment'] . "' WHERE num_dev=$num_devis";
		mysql_query($sql_ht) OR die("Erreur mise à jour du commentaires <br/>$sql_ht<br/>" . mysql_error() );
	}

	if (isset($_POST['reference']))
	{
		//la page à été appelé depuis une mise à jour de la reference
		//la reference doit donc etre mis à jour
		$sql_ht = "UPDATE ${tblpref}devis SET reference='" . $_POST['reference'] . "' WHERE num_dev=$num_devis";
		mysql_query($sql_ht) OR die("Erreur mise à jour de la reference <br/>$sql_ht<br/>" . mysql_error() );
	}

}

require_once("include/headers.php"); // pas avant car il y a un header(Location )
require_once("include/categorie_choix.php");

?>
<script language="JavaScript" type="text/javascript">
	function confirmDelete()
	{
	var agree=confirm("<?php   echo 'Désirer vous vraiment effacer cette ligne du devis ?'; ?>");
	if (agree)
		return true ;
	else
		return false ;
	}
</script>
<script language="javascript" src="javascripts/verifier_form.js" type="text/javascript"></script>
<script type="text/javascript">
	function verif_date_devis()
	{
		if (verifier('frm_date','black','date','vide#date_'))
		{
			document.frm_date.submit();
		} else {
			alert("<?php aff_lang('Une des valeurs saisie n\'est pas correct.\nMerci de corriger avant de poursuivre.') ?>")	
		}

	}
</script>
<script type="text/javascript" src="javascripts/champ_ro_rw.js"></script>

<?php
//echo "numdev=$num_devis";
$sql = "SELECT * FROM ${tblpref}devis 
	RIGHT join ${tblpref}client on ${tblpref}devis.client_num = ${tblpref}client.num_client
	WHERE num_dev = $num_devis";

$req = mysql_query($sql) or die("Erreur selection commentaires !<br/>$sql<br/>".mysql_error());
$data = mysql_fetch_array($req);

$num = $data['client_num'];
$nom = $data['nom'];

$coment = htmlentities($data['coment'], ENT_QUOTES,'UTF-8');
$reference = htmlentities($data['reference'], ENT_QUOTES,'UTF-8');

?>


<table class="page" align="center">
 	<tr>
 		<td class="page" align="center">
 			<?php
 			require_once("include/head.php");
 			?>
 		</td>
 	</tr>
	<tr>
		<td class="page">
		<center>

		<?php
			//a chaque ouverture de ce document on recalcule le montant total du devis.
			// pourquoi ? car cette page est appelé apres l'ajout,suppression,modification d'article
			$sql_sum_devis = "SELECT sum(tot_art_htva) as tot_art_htva , sum(to_tva_art) as to_tva_art
						FROM ${tblpref}cont_dev WHERE dev_num = $num_devis";
			$result=mysql_query($sql_sum_devis) OR die("Erreur calcul totaux<br/>$sql_sum_devis<br/>" . mysql_error());
			$data=mysql_fetch_array($result);
			
			$total_ht=$data['tot_art_htva'];
			$total_tva=$data['to_tva_art'];

			$sql_tot_devis = "UPDATE ${tblpref}devis SET tot_htva='$total_ht',tot_tva='$total_tva' 
						WHERE num_dev=$num_devis";
			mysql_query($sql_tot_devis) OR die("Erreur mise à jour totaux<br/>$sql_tot_devis<br/>" . mysql_error());


			$sql = "SELECT date FROM ${tblpref}devis WHERE num_dev=$num_devis";
			$req = mysql_query($sql) or die("Erreur SQL sélection devis n°$num_devis !<br/>$sql<br/>" . mysql_error());
			$data = mysql_fetch_array($req);
			$date=sql_to_date($data['date']);

			$total_ttc = $total_ht + $total_tva;

		?>

			<table width="70%">
				<caption><?php echo get_lang("Edition du devis n° ") . $num_devis ?></caption>
				<tr>
					<td class="grand"><?php aff_lang("Client"); ?></td>
					<td class="grand"><?php echo $nom; ?></td>
					<td class="grand"><?php aff_lang("Montant HT"); ?></td>
					<td class="grand" style="text-align:right"><?php echo avec_virgule($total_ht) ?></td>
					<td>&nbsp;</td>
				</tr>

				<tr>
					<td class="grand" id="date"><?php aff_lang("Date"); ?></td>
					<td>
						<form action="devis_modif_date.php" method="post" name="frm_date">
							<input type="hidden" value="<?php echo $num_devis ?>" name="num_devis"/>
							<input type="text" name="date" class="grand" value="<?php echo $date ?>" size="7" />
							<a href="#" onclick="window.open('include/pop.calendrier.php?frm=frm_date&amp;ch=date','calendrier','width=415,height=190,left=200,top=200,scrollbars=0').focus();">
								<img src="image/petit_calendrier.gif" alt="calendrier" border="0"/>
							</a>
							<a href="#" onclick="verif_date_devis()" ><img src="image/ok_kde.png" border="0" alt="valider" /></a>
						</form>
					</td>
					

					<td class="grand"><?php aff_lang("Montant TVA"); ?></td>
					<td class="grand" style="text-align:right"><?php echo avec_virgule($total_tva) ?></td>
					<td>&nbsp;</td>
				</tr>

				<tr>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td class="grand"><?php aff_lang("Montant TTC"); ?></td>
					<td class="grand" style="text-align:right"><?php echo avec_virgule($total_ttc) ?></td>
					<td>&nbsp;</td>
				</tr>

			</table>

			<br/>


			<table class="boiteaction">
				<tr>
					<th><?php echo $lang_quantite ;?></th>
					<th><?php echo $lang_article ;?></th>
					<th width="10"><?php echo $lang_editer ;?></th>
					<th width="10%"><?php aff_lang("Mnt HT") ?></th>
					<th><?php aff_lang("Tx TVA") ?></th>
					<th><?php aff_lang("Mnt TTC") ?></th>
					<th><?php echo $lang_supprimer ;?></th>
				</tr>
				<?php



				/*$sql = "SELECT ${tblpref}cont_dev.num, quanti, uni, article, tot_art_htva, to_tva_art tva,article_num 
					FROM ${tblpref}cont_dev RIGHT JOIN ${tblpref}article on ${tblpref}cont_dev.article_num = ${tblpref}article.num
					WHERE dev_num = $num_devis ORDER BY ${tblpref}cont_dev.num";*/
				$sql = "SELECT *,${tblpref}cont_dev.num as num_cont FROM ${tblpref}cont_dev
						INNER JOIN ${tblpref}article ON article_num=${tblpref}article.num
						WHERE dev_num=$num_devis ORDER BY num_cont";
				$req = mysql_query($sql) or die("Erreur selection du devis !<br/>$sql<br/>" . mysql_error());

				//$total = 0.0;
				//$total_dev = 0.0;
				//$total_tva = 0.0;
			
				while($data = mysql_fetch_array($req))
				{
					$quanti = $data['quanti'];
					//$uni = $data['uni'];
					//$article = $data['article'];
					$num_article = $data['article_num'];
					$tot_art_htva = $data['tot_art_htva'];
					//$tva = $data['tva'];
					$num_cont = $data['num_cont'];
					$tx_tva = $data['taux_tva'];

					//$prix_htva = $data['prix_htva'];

					$prix_ttc = $tot_art_htva * (1 + $tx_tva/100);
					//$total_dev += $tot;
					//$total_tva += $tva;
						
				?>
				<form name="ligne_num_<?php echo $num_cont ?>" id="ligne_num_<?php echo $num_cont ?>" action="devis_modif_article.php" method="post">
					<tr>
						<td class='<?php echo couleur_alternee (TRUE); ?>' style="text-align:right;padding-right:5px">
							<input type="text" size="8" readonly="readonly" style="text-align:right" class="champ_modif_ro" onclick="champ_modif_rw('ligne_num_<?php echo $num_cont ?>')" value="<?php echo $quanti ?>" name="quantite" />
						</td>
						<td class='<?php echo couleur_alternee (FALSE); ?>'>
							<center>
								<?php combo_choix_categorie("ligne_num_$num_cont","ro",$num_article); ?>
							</center>
						</td>
						<td class='<?php echo couleur_alternee (FALSE); ?>'>
							<input type="hidden" name="num_cont" value="<?php echo $num_cont ?>" />
							<input type="hidden" name="num_devis" value="<?php echo $num_devis ?>" />
							<center>
								<a id="ligne_num_<?php echo $num_cont ?>_modif" href="#" onclick="champ_modif_rw('ligne_num_<?php echo $num_cont ?>')" ><img border="0" src="image/edit.png" alt="Editer" /></a>
								<input type="image" id="ligne_num_<?php echo $num_cont ?>_validation" src="image/ok_kde.png" style="visibility:hidden" />
							</center>
						</td>
						<td class='<?php echo couleur_alternee (FALSE); ?>' style="text-align:right;">
							<?php echo montant_financier ($tot_art_htva); ?> &nbsp;
						</td>
						<td class='<?php echo couleur_alternee (FALSE); ?>' style="text-align:right;">
							<?php echo montant_taux ($tx_tva); ?> &nbsp;
						</td>
						<td class='<?php echo couleur_alternee (FALSE); ?>' style="text-align:right;">
							<?php echo montant_financier ($prix_ttc); ?> &nbsp;
						</td>
						<td class='<?php   echo couleur_alternee (FALSE); ?>'>
							<a href="devis_sup_article.php?num_cont=<?php echo $num_cont ?>&amp;num_devis=<?php echo $num_devis ?>" onclick='return confirmDelete()'>
								<img border="0" src="image/delete.png" alt="delete"/>
							</a>
						</td>
					</tr>
				</form>
				<?php
				//$total += $tot;
				}
				?>
			
				<tr>
					<td class='totalmontant' colspan="2"><?php echo $lang_total; ?></td>
					<td class='totalmontant' colspan="2"><?php echo montant_financier ($total_ht); ?></td>
					<td class='totaltexte'>&nbsp;</td>
					<td class='totalmontant'><?php echo montant_financier ($total_ttc); ?></td>
					<td colspan='4' class='totaltexte'>&nbsp;</td>
				</tr>
				<?php
				//on calcule la somme des contenus du devis
				//$sql = " SELECT SUM(tot_art_htva) FROM ${tblpref}cont_dev WHERE dev_num = $num_devis";
				//$req = mysql_query($sql) or die("Erreur SQL !<br/>$sql<br/>".mysql_error());
				?>
			
			</table>
			
			<br/><hr/><br/>

			<form name="formu_add_article" method="post" action="devis_add_article.php">
				<table class="boiteaction">
					<caption>
						<?php aff_lang("Ajouter un article au devis."); ?>
					</caption>
		
					<tr>
						<td class="texte0"><?php echo $lang_article; ?></td>
						<td class="texte0" >
							<?php
								//si le user viens d'ajouter un article on reselectionne le mm article : Cela correspond au dernier article de la liste
								$der_article_aff=isset($num_article)?$num_article:1; //si des articles dans ce devis, selectionne le dernier artcile
								combo_choix_categorie("formu_add_article","",$der_article_aff);
							?>
						</td>
					</tr>
					<tr>
						<td class="texte0"><?php echo $lang_quantite; ?> </td>
						<td class="texte0"><input name="quanti" type="text" id="quanti" size="6" /></td>
					</tr>
					<tr>
						<td class="submit" colspan="2"> 
							<input name="num_devis" type="hidden" id="nom" value='<?php echo $num_devis; ?>' />
							<input type="submit" name="Submit2" value='<?php echo $lang_devis_ajouter; ?>' />
						</td>
					</tr>

				</table>
			</form>

			<br/><hr/><br/>

			<form action="devis_edit.php" method="post" name="reference">
				<table class="boiteaction">
					<caption>
						<?php aff_lang("Ajouter une référence au devis") ?>.
					</caption>
					<tr>
						<td class="texte0">
							<br/>
							<input type="text" name="reference" size="80" maxlength="80" value="<?php echo $reference ?>" />
							
							<input type="submit" name="Submit" value='<?php aff_lang("Enregistrer la référence") ?>' />
						</td>
					</tr>
				</table>
				<input type="hidden" name="num_devis" value='<?php echo $num_devis; ?>' />
			</form>

			<br/><hr/><br/>

			<form action="devis_edit.php" method="post" name="fin_dev">
				<table class="boiteaction">
					<caption>
						<?php echo $lang_ajo_com_dev; ?>.
					</caption>
					<tr>
						<td class="submit">
							<br/>
							<textarea name="coment" cols="45" rows="3"><?php echo $coment; ?></textarea>
							<br/>
							<input type="submit" name="Submit" value='<?php aff_lang("Enregistrer le commentaire") ?>' />
						</td>
					</tr>
				</table>
				<input type="hidden" name="num_devis" value='<?php echo $num_devis; ?>' />
			</form>


		</center>
		</td>
	<?php
	//les totaux sont enregistrer à chaque chargement de la page
	//$sql_ht = "UPDATE ${tblpref}devis SET tot_htva='$total_dev',tot_tva='$total_tva' WHERE num_dev=$num_devis";
	//mysql_query($sql_ht) OR die("<p>Erreur Mise à jour montant tota devis<br/>$sql_ht<br/>" . mysql_error() . "</p>");
	?>
	</tr>
	<tr>
		<td>
			<?php
			require_once("include/bas.php");
			?>
		</td>
	</tr>

</table>

</body>
</html>
